<template>
  <view>
    <view class="input-search">
		<scanCode :scanType="4"></scanCode>
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" placeholderStyle="font-size:17px;"></uni-easyinput>
    	<button @click="search">搜索</button>
    </view>
    <uni-table ref="table" :type="flag=='add'||config.online?'':'selection'" stripe :loading="loading" border emptyText="暂无更多数据" filter-type="search"@selection-change="selectionChange">
		<uni-tr v-show="flag=='add'||config.online==1?false:true">
			<uni-th>
				<button class="close_btn del_btn" @click="toDelete">删除</button>
			</uni-th>
		</uni-tr>
		<uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-td width="100%" align="left" class="td-class" >
				<div @click="selectes(item)" @longpress="longPressDelete(index)" class="td_div">
					<!-- <view class="td_list">
						<text class="label">进度：</text>
						<progress :percent="item.rat" show-info stroke-width="15" activeColor="#18bc9c" backgroundColor="#999" style="width: 82%;"></progress>
					</view> -->
					<view class="td_list">
						<text class="label">订单编号：</text>
						<text class="text">{{ item.pcode }}</text>
					</view>
					<view class="td_list">
						<text class="label">单据日期：</text>
						<text class="text font-red">{{ item.pdate }}</text>
					</view>
					<view class="td_list" v-show="item.type==1">
						<text class="label">{{item.type==1?'客户':'申请人'}}：</text>
						<text class="text">{{ item.cUser_Name }}</text>
					</view>
					<view class="td_list" v-show="item.type==1">
						<text class="label">部门：</text>
						<text class="text">{{ item.cdepname }}</text>
					</view>
					<view class="td_list">
						<text class="label">批号：</text>
						<text class="text">{{ item.cBatch }}</text>
					</view>
					<view class="td_list">
						<text class="label">物料：</text>
						<text class="text">{{ item.inv_name }}</text>
					</view>
					<view class="td_list">
						<text class="label">编码：</text>
						<text class="text">{{ item.inv_number }}</text>
					</view>
					<view class="td_list">
						<text class="label">规格：</text>
						<text class="text">{{ item.inv_specification }}</text>
					</view>
					
					<view class="td_list">
						<text class="label">单位：</text>
						<text class="text">{{ item.unit_name }}</text>
						<text class="label">常用单位：</text>
						<text class="text">{{ item.store_unit }}</text>
					</view>
					<!-- <view class="td_list">
						<text class="label">推荐仓库：</text>
						<text class="text font-red">{{ item.warehouse_name }}</text>
					</view>
					<view class="td_list">
						<text class="label">推荐路径：</text>
						<text class="text font-red">{{ item.tj_shelves_name }}</text>
					</view> -->
					<view class="td_list">
						<text class="label">应出：</text>
						<text class="text">{{ item.quantity }}</text>
						<text class="label">待出：</text>
						<text class="text font-red">{{ item.no_quantity }}</text>
					</view> 
					<view class="td_list">
						<text class="label">常用应出：</text>
						<text class="text">{{ item.quantity/item.fCoefficient }}</text>
						<text class="label">待出：</text>
						<text class="text font-red">{{ item.no_quantity/item.fCoefficient }}</text>
					</view>
					<!-- <view class="td_list">
						<text class="label">状态：</text>
						<text class="text font-red">{{ item.fclose == 1?'关闭':'有效' }}</text>
					</view> -->
					<view class="td_list">
						<text class="label">备注：</text>
						<text class="text">{{ item.remark }}</text>
					</view>
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
  </view>
</template>

<script>
// 引入uni-ui组件
import scanCode from '@/component/scan-code.vue'
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput
} from "@dcloudio/uni-ui"
export default {
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
      selectedIndexs:[],//选中: '',
      
	  styles:{
	  	borderColor: '#999'
	  },
	  keyword:'',
	  where:' 1=1 ',
	  flag:'',//新增标识
	  order_type:'', //单据类型 2材料出库 5销售出库
	  qtyDecimal:this.globalData.qtyDecimal,
	  
	  //在线版
	  config:uni.getStorageSync('myconfig'),
	  pageinfo:{},
	  //
    }
  },
  components: {
    uniPagination,
    uniIcons, //uni-ui的icons组件
    uniCard, //uni-ui的卡片组件
    uniTable,
    uniTr,
    uniTh,
    uniTd,
	uniEasyinput,scanCode
  },
  onPullDownRefresh() {
  	this.getlist()
  	uni.stopPullDownRefresh();
  },
  methods: {
    // 点击关闭的事件回调
    close() {
      uni.navigateBack({})
    },
	// 搜索
	search() {
		this.currentPage = 1
		this.getlist();
	},
	longPressDelete(index){
		//在线版不给删
		if(this.flag=='add'||this.config.online==1){
			return
		}
		
		this.selectedIndexs = []
		this.selectedIndexs.push(index)
		this.toDelete()
	},
    //获取列表
    getlist() {
		uni.showLoading();
		if(!this.config.online){
			if(this.keyword!=''){
				this.where = " inv_name like '%"+this.keyword+"%' or inv_specification like '%"+this.keyword+"%' or inv_number like '%"+this.keyword+"%' or pcode like '%"+this.keyword+"%' or remark like '%"+this.keyword+"%'"
			}else{
				this.where = ' 1=1 '
			}
			if(this.flag=='add'){
				this.where += " and fclose = 0 " //添加源单时 只显示未关闭的计划单
			}
			if(this.flag=='back'){
				this.where += " and commit_quantity > 0 " //退货 已出数量大于0
			}
			
			this.$Db.queryCount('appmaterial',this.where).then(res => {
				this.total = res[0].num;//获得总页数
			})
			this.$Db.queryDataList('appmaterial',this.currentPage,this.pageSize,this.where,'id','desc').then(res => {
				this.card_arr = [];
				res.forEach((item,index)=>{
					item.rat = parseInt(item.commit_quantity*100/item.quantity)
					item.no_quantity = (item.quantity - item.commit_quantity).toFixed(this.qtyDecimal)
					this.card_arr.push(item)
				})
				uni.hideLoading();
			})
		}else{
			//20231204在线版
			let type = 3 //'销售出库'
			if(this.order_type == 2){
				type = 2 //'投料清单'
			}
			let condition = {
				 keyword:this.keyword,
				 type:type
			}
			
			if(this.flag=='add'){
				condition.status = 1 //添加源单时 只显示未关闭的出库任务
			}
			
			this.pageinfo = {
				table:'outask',
				pagesize:this.pageSize,
				page:this.currentPage,
				order_str:'',
				condition_str:JSON.stringify(condition)
			}
			
			this.$Api.getPageList(this.pageinfo).then(res=>{
				this.card_arr = res.data.data;
				this.total = res.data.total
				this.currentPage = res.data.current_page
				uni.hideLoading();
			}).catch(err=>{
				this.$Api.msg(err)
			})
			//
		}
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    },
	// 多选
	selectionChange(e) {
		this.selectedIndexs = e.detail.index
	},
	selectes(item) {
	  if (item == '') return
	  if(this.flag != undefined ){
		  //监听
		  uni.navigateBack({
			delta: 1,
			success: () => {
			  uni.$emit("getAppmaterial", item)
			}
	  });
	  }
	},
	
	toDelete(){
		let _this = this;
		let delFlag = true;
		let source_code = ''
		let i = 0
		
		uni.showModal({
		    title: '确定要删除记录？',
		    //content: '确定要删除记录？',
		    success: function (res) {
		        if (res.confirm) {
					if(_this.selectedIndexs.length>0){
						 _this.selectedIndexs.forEach(key=>{
							 _this.$Db.queryCount('stockbill',"source_code='"+_this.card_arr[key].pcode+"'").then(nres => {
								i++
								if(nres[0].num){
									source_code = _this.card_arr[key].pcode
							 		delFlag = false;
							 	}
							 })
						 })
						 
						 let time = setInterval(()=>{
							 if(i == _this.selectedIndexs.length){
								 clearInterval(time)
								 if(!delFlag){
								 	//不能删除，存在下游单据
								 	_this.$Api.msg("单号["+source_code+"]存在下游单据,请删除")
									_this.$refs.table.clearSelection()
								 	return
								 }else{
									 //删除采购订单
									 _this.selectedIndexs.forEach(key=>{
										 _this.$Db.deleteTableData('appmaterial','id',_this.card_arr[key].id).catch(err=>{
											 console.log(err)
											 _this.$Api.msg("删除失败",'error')
											 return false
										 })
									 })
									 
									 _this.$refs.table.clearSelection()
									 _this.getlist()
									 
									 setTimeout(()=>{
										 uni.hideLoading()
										 _this.$Api.msg('删除成功','success')
									 },500)
								 }
							 }
						 },500)
					}
		        }
		    }
		});
	}
  },
  mounted(e) {},
  onLoad(e) {
	this.flag = e.flag
	this.order_type = e.order_type
	this.getlist()
	//更新标题
	this.$Func.setBarTitle('出库任务')
	
	/* 监听传过来的扫描单号 */
	uni.$on("scan_erp_order", (value) => {
		//this.keyword = value;
		let arr = value.split('!');
		if(arr.length>1){
			this.keyword = arr[0].substr(2)
		}else{
			this.keyword = value;
		}
		this.search()
	})
  },
  destroyed() {
  	uni.$off("scan_erp_order")
	this.globalData.toSecPage = ''
  }
}
</script>
<style lang="less">
</style>